﻿using BlogApi.Data;
using BlogApi.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BlogApi.Services
{
    /// <summary>
    /// 用户仓储接口实现类
    /// </summary>
    public class UserRepository:IUserRepository
    {
        private readonly BlogDbContext _context;

        public UserRepository(BlogDbContext context)
        {
            _context = context ?? throw new ArgumentNullException(nameof(context));
        }
        public async Task<IEnumerable<User>> GetUsersAsync()
        {
            return await _context.Users_table.OrderByDescending(item => item.CreateTime).ToListAsync();
        }
        public async Task<IEnumerable<User>> GetUsersByPageAsync(int pageSize,int pageIndex)
        {
            return await _context.Users_table.OrderByDescending(item => item.CreateTime).Skip(pageSize * pageIndex).Take(pageSize).ToListAsync();
        }
        public async Task<IEnumerable<User>> GetUsersByPageOrderByFanNumberAsync(int pageSize, int pageIndex)
        {
            return await _context.Users_table.OrderByDescending(item => item.FanNumber).Skip(pageSize * pageIndex).Take(pageSize).ToListAsync();
        }
        public async Task<User> GetUserAsync(Guid userId)
        {
            if (userId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(userId));
            }
            return await _context.Users_table.FirstOrDefaultAsync(u => u.Id == userId);
        }
        public async Task<User> GetUserAsync(string email)
        {
            return await _context.Users_table.FirstOrDefaultAsync(u => u.Email == email);
        }
        public async Task<User> GetUserByEmailAndPasswordAsync(string email, string password)
        {
            return await _context.Users_table.FirstOrDefaultAsync(u => u.Email == email && u.Password == password);
        }
        public void AddUser(User user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            _context.Users_table.Add(user);
        }
        public void UpdateUser(User user)
        {
            //由于数据库上下文会对实体进行跟踪 所以这句话可以不写
            //_context.Entry(user).State = EntityState.Modified;
        }
        public void DeleteUser(User user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            _context.Users_table.Remove(user);
        }
        public async Task<bool> UserExistsAsync(Guid userId)
        {
            if (userId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(userId));
            }
            return await _context.Users_table.AnyAsync(u => u.Id == userId);
        }
        public async Task<bool> EmailExistsAsync(string email)
        {
            return await _context.Users_table.AnyAsync(u => u.Email == email);
        }
        public async Task<bool> SaveAsync()
        {
            return await _context.SaveChangesAsync()>=0;
        }
    }
}
